/**
 * @file       PagingStrategy.cpp
 * @brief      TODO
 * @author     Huabo Zheng
 * @date       Jan 31, 2012
 * @Copyright  2012, www.netgear.com Inc. All rights reserved.
 */

#include "PagingStrategy.h"
#include "errorcode.h"
#include "PageInfo.h"

namespace NS_LCD_AFW
{

CPagingStrategy::CPagingStrategy()
{
}

CPagingStrategy::~CPagingStrategy()
{
}

int CPagingStrategy::calculatePaging(const CFramePtrArray & framePtrArray,
		const CRect &parentRect, CPageInfo & pageResult)
{

	int pageTotal = 1;
	CPoint pt;
	for (std::size_t i = 0; i < framePtrArray.size(); ++i)
	{
//		pt = framePtrArray[i]->getRect().getRightBottom();
//		if (pt.getY() > parentRect.getRightBottom().getY() * pageTotal)
//		{
//			++pageTotal;
//		}
		if (framePtrArray[i]->getRect().getHight()
				+ framePtrArray[i]->getRect().getTop()
				> parentRect.getHight() * (pageTotal))
		{
			++pageTotal;
		}
	}
	pageResult.setTotalPage(pageTotal);

	int iStartIndex = 0;
	std::size_t idx = 0;
	for (int curPage = 0; curPage < pageTotal; ++curPage)
	{
		iStartIndex = idx;
		for (; idx < framePtrArray.size(); ++idx)
		{
			//pt = framePtrArray[idx]->getRect().getRightBottom();
//			if (pt.getY() > parentRect.getRightBottom().getY() * (curPage + 1))
//			{
//				break;
//			}
			if (framePtrArray[idx]->getRect().getHight() + framePtrArray[idx]->getRect().getTop()
						> parentRect.getHight() * (curPage + 1))
			{
				break;
			}
		}
		pageResult.setPageInfo(curPage + 1, iStartIndex, idx);
	}

	return ERR_SUCCESS;
}

int CPagingStrategy::findNextHightlightableObjectIndex(
		const CFramePtrArray & framePtrArray, int iStart, int iEnd ,bool bFindBackward )
{
	if (!bFindBackward)
	{
		for (int i = iStart;
				i >= iEnd && i >= 0 && i < (int) framePtrArray.size(); --i)
		{
			if (/*framePtrArray[i]->isHightlight() ||*/framePtrArray[i]->isHightlightable())
			{
				return i;
			}
		}
	}
	else
	{
		for (int i = iStart;
				i < iEnd && i >= 0 && i < (int) framePtrArray.size(); ++i)
		{
			if (framePtrArray[i]->isHightlightable())
			{
				return i;
			}
		}
	}

	return -1;
}

} /* namespace NS_LCD_AFW */
